package me.qi.kancha.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.qi.kancha.dto.base.BaseDTO;
import me.qi.kancha.dto.enums.AccountStatus;
import me.qi.kancha.dto.enums.AccountType;
import me.qi.kancha.exception.MessageException;
import me.qi.kancha.mapper.AccountMapper;
import me.qi.kancha.service.AccountService;
import me.qi.kancha.dto.core.AccountDTO;
import me.qi.kancha.dto.param.ModifyPasswordParam;
import me.qi.kancha.exception.PreconditionException;
import me.qi.kancha.vo.account.AccountInsertVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author kobe
* @description 针对表【account】的数据库操作Service实现
* @createDate 2022-01-06 14:51:47
*/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountDTO>
    implements AccountService {

    private final PasswordEncoder passwordEncoder;

    private final AccountMapper accountMapper;

    public AccountServiceImpl(PasswordEncoder passwordEncoder, AccountMapper accountMapper) {
        this.passwordEncoder = passwordEncoder;
        this.accountMapper = accountMapper;
    }

    @Override
    public Boolean modifyPassword(ModifyPasswordParam param) {
        AccountDTO accountDTO = accountMapper.selectById(param.getId());
        if (accountDTO == null) {
            throw new PreconditionException("用户不存在，id=" + param.getId());
        }

        boolean matches = passwordEncoder.matches(param.getOldPwd(), accountDTO.getPassword());
        if (!matches) {
            throw new PreconditionException("用户原密码不正确，id=" + param.getId());
        }

        String encodePassword = passwordEncoder.encode(param.getNewPwd());

        LambdaUpdateWrapper<AccountDTO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(AccountDTO::getId, param.getId())
                .set(AccountDTO::getPassword, encodePassword);

        int update = accountMapper.update(null, lambdaUpdateWrapper);
        return update > 0;
    }

    @Override
    public AccountDTO insert(Object paramObj) {
        AccountInsertVo param = (AccountInsertVo) paramObj;
        boolean exists = accountMapper.exists(new LambdaQueryWrapper<AccountDTO>()
                .eq(AccountDTO::getUsername, param.getUsername()));
        if (exists) {
            throw new MessageException("用户名已存在");
        }
        long count = count(new LambdaQueryWrapper<AccountDTO>()
                .ge(BaseDTO::getCreatedTime, DateUtil.beginOfDay(new Date())));
        if (count > 100) {
            throw new MessageException("注册数达到上限");
        }
        AccountDTO dto = new AccountDTO();
        dto.setStatus(AccountStatus.NORMAL);
        dto.setType(AccountType.PASSWORD);
        dto.setUsername(param.getUsername());
        dto.setPassword(passwordEncoder.encode(param.getPassword()));
        save(dto);
        return dto;
    }
}




